package com.erp.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;

public class SignUtil {
    private static final String APP_KEY = "your_app_key"; // 实际应用中应该配置在配置文件中

    public static String generateSign(String appId, String goodsId, String reqId, Timestamp reqTime) {
        String content = appId + goodsId + reqId + reqTime + APP_KEY;
        return md5(content);
    }

    public static String md5(String content) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(content.getBytes());
            return bytesToHex(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}
